<template>
  <a-modal :title="title" width="1100px" :maskClosable="false" :visible="dialog" :confirm-loading="loading" @ok="handleSave" @cancel="cancelForm">
    <div>
      只有订单未付款时才支持改价，改价后请联系买家刷新订单核实订单金额后再支付。
    </div>
    <div style="padding:15px 0;">
      <b>运费：</b>
      <a-input-number :min="0" v-model="selectData.freight" size="small" />
    </div>
    <div>
      <a-table :columns="columns" :pagination="false" rowKey="id" bordered :data-source="selectData.skuList">
        <template slot="changePriceInput" slot-scope="text, record">
          <a-input-number v-model="record.changePrice"
            :min="0-record.subTotal+0.01+record.couponDiscount*1+record.shopCouponDiscount*1+record.integralDiscount*1|numToFixed2" size="small" />
        </template>
        <template slot="youhui" slot-scope="text, record">
          {{record.couponDiscount*1+record.shopCouponDiscount*1+record.integralDiscount*1|numToFixed2}}
        </template>
      </a-table>
    </div>
    <div>
      收货信息：{{selectData.addressInfoObj.name}}-{{selectData.addressInfoObj.tel}}
    </div>
    <div>
      实付金额 = 小计 - 优惠 + 涨价或减价 + 运费
    </div>
    <div><b>实付金额：{{amount}}</b></div>

  </a-modal>
</template>

<script>
import { postAction } from '@/api/manage'
export default {
  name: 'UpdateModal',
  data () {
    return {
      columns: [
        {
          title: '商品',
          dataIndex: 'spuName'
        },
        {
          title: '单价（元）',
          dataIndex: 'skuPrice'
        },
        {
          title: '数量',
          dataIndex: 'num'
        },
        {
          title: '小计（元）',
          dataIndex: 'subTotal'
        },
        {
          title: '优惠',
          dataIndex: 'discount',
          scopedSlots: { customRender: 'youhui' },
        },
        {
          title: '涨价或减价',
          dataIndex: 'changePrice',
          scopedSlots: { customRender: 'changePriceInput' },
        }


      ],
      type: 1,
      title: '',
      start_page: 0,
      pages: 200,
      selectData: {
        addressInfoObj: {},
        skuList: []
      }, // 可选择上级菜单
      dialog: false,
      loading: false,
      ruleForm: {
        orderNo: "",
        freight: 0,
        skus: []
      }
    }
  },
  computed: {
    // 计算属性的 getter
    amount: function () {
      let sum = 0
      for (let i = 0; i < this.selectData.skuList.length; i++) {
        sum = sum + parseFloat(this.selectData.skuList[i].subTotal) - parseFloat((this.selectData.skuList[i].shopCouponDiscount * 1 + this.selectData.skuList[i].couponDiscount * 1 + this.selectData.skuList[i].integralDiscount * 1) || 0) + parseFloat((this.selectData.skuList[i].changePrice == '-' ? 0 : this.selectData.skuList[i].changePrice) || 0)
      }
      sum = (sum + parseFloat((this.selectData.freight == '-' ? 0 : this.selectData.freight) || 0)).toFixed(2)
      return sum
    }
  },
  created () {

  },
  methods: {

    //   查询数据

    edit (record) {
      this.dialog = true
      this.loading = false
      this.ruleForm.freight = 0
      this.ruleForm.skus = []
      this.selectData = Object.assign({}, record)
      this.title = '订单原价（含运费）：' + record.originalAmount + '元'
      // this.loadSelectData()
    },
    handleSave () {
      const self = this
      if (self.loading) {
        return
      }
      self.loading = true
      self.ruleForm.skus = []
      self.ruleForm.freight = this.selectData.freight || 0
      for (let i = 0; i < self.selectData.skuList.length; i++) {
        let obj = {
          id: self.selectData.skuList[i].id,
          changePrice: self.selectData.skuList[i].changePrice || 0
        }
        self.ruleForm.skus.push(obj)
      }

      const thisForm = Object.assign({}, self.ruleForm)

      postAction('/mallBase/omsOrder/updatePrice', thisForm).then(res => {
        self.$emit('ok')
        self.loading = false
        self.dialog = false
      }).catch(() => {
        self.loading = false
      })
    },
    cancelForm () {
      this.loading = false
      this.dialog = false
    }
  }
}
</script>

<style lang="less" scoped>
/** Button按钮间距 */
</style>
